package com.lej.app_interact

import android.graphics.Color
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.app_interact.databinding.*
import com.lej.core.base.BaseFragment
import com.lej.core.dsl.dp
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.*
import com.lej.core.widget.itemDecoration.DividerDecoration
import com.lej.dataservice.club.bean.interact.FeedInfo
import com.qmuiteam.qmui.kotlin.onClick
import kotlinx.coroutines.launch

@Route(path = CRouter.interactFra)
class InteractFragment : BaseFragment<InteractFragmentInteractBinding, InteractViewModel>() {
    private lateinit var adapter: QuickMultiAdapter<FeedInfo>

    override fun getLayout(): Int = R.layout.interact_fragment_interact

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        getFeedListData()
        binding.ivAuthorSearch.onClick {
            getFeedListData()
        }
        binding.rlvFeedList.addItemDecoration(
            DividerDecoration.builder().color(Color.TRANSPARENT)
                .height(10.dp)
                .build()
        )
        adapter = binding.rlvFeedList.generateMultiQHolder(
            MultiHolder(R.layout.interact_item_feed_text, type = FeedInfo.FEED_NORMAL) { holder, item ->
                holder.getBind<InteractItemFeedTextBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_feed_text, type = FeedInfo.FEED_LINK) { holder, item ->
                holder.getBind<InteractItemFeedTextBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_feed_video, type = FeedInfo.FEED_VIDEO) { holder, item ->
                holder.getBind<InteractItemFeedVideoBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_feed_pic, type = FeedInfo.FEED_PIC) { holder, item ->
                holder.getBind<InteractItemFeedPicBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_feed_activity, type = FeedInfo.ACTIVITY_NORMAL) { holder, item ->
                holder.getBind<InteractItemFeedActivityBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_activity_audio, type = FeedInfo.ACTIVITY_AUDIO) { holder, item ->
                holder.getBind<InteractItemActivityAudioBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            MultiHolder(R.layout.interact_item_activity_video, type = FeedInfo.ACTIVITY_VIDEO) { holder, item ->
                holder.getBind<InteractItemActivityVideoBinding>()?.apply {
                    info = item
                    includeHead.info = item
                    includeFoot.info = item
                }
            },
            getType = { it.getLocFeedType() }, loadType = LOAD_TYPE_UP_AND_LOAD
        )

        adapter.loadMoreModule.setOnLoadMoreListener {
            getFeedListData()
        }
        adapter.upFetchModule.isUpFetchEnable = true
        adapter.upFetchModule.setOnUpFetchListener {
            viewModel.feedTimestamp = 0L
            getFeedListData()
        }
    }

    private fun getFeedListData() {
        lifecycleScope.launch {
            kotlin.runCatching {
                val list = viewModel.getInteractData()
                adapter.setList(list)
            }.onFailure {
                if (viewModel.feedTimestamp == 0L) {//

                }
            }
        }
    }

}